﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BTKERP.DAL.DataAccess
{
    public class ExSQLCommand
    {
        private SqlCommand _cmd;
        private Dictionary<string, object> _pms;

        #region Properties
        public string CommandText
        {
            get{ return _cmd.CommandText; }
            set { _cmd.CommandText = value; } 
        }
        public CommandType Type
        {
            get { return _cmd.CommandType; }
            set { _cmd.CommandType = value; }
        }
        public SqlCommand cmd
        {
            get
            {
                if (_pms.Count == 0) return _cmd;
                else
                {
                    SqlParameter sp;
                    foreach (KeyValuePair<string, object> parameter in _pms)
                    {
                        sp = new SqlParameter(parameter.Key, parameter.Value);
                        _cmd.Parameters.Add(sp);
                    }
                    return _cmd;
                }
            }
        }
        public SqlConnection Connection
        {
            get { return _cmd.Connection; }
            set { _cmd.Connection = value; }
        }
        #endregion

        public ExSQLCommand(string name)
        {
            _cmd = new SqlCommand();
            _pms = new Dictionary<string, object>();
            _cmd.CommandText = name;
            _cmd.CommandType = CommandType.StoredProcedure;
        }

        public ExSQLCommand(string command, CommandType type)
        {
            _cmd = new SqlCommand();
            _pms = new Dictionary<string, object>();
            _cmd.CommandText = command;
            _cmd.CommandType = type;
        }

        public void AddParameter(string name, object value)
        {
            if (value != null)
            {
                if (!_pms.ContainsKey(name)) { _pms.Add(name, value); }
                else { _pms[name] = value; }
            }
        }
    }
}
